package com.wl.eb.controller;

import com.wf.captcha.utils.CaptchaUtil;
import com.wl.eb.module.EbUser;
import com.wl.eb.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/12/7 10:02
 */
@Controller//证
@RequestMapping("login")//工号
public class LoginController {

    /**
     * 返回登录页面
     * @return
     */
    @GetMapping("/on")
    public String loginPage(){
        return "login";//templates/login.html
    }


    /**
     * 返回注册页面
     * @return
     */
    @GetMapping("/reg")
    public String loginReg(){
        return "register";//templates/login.html
    }
    @Resource
    private LoginService loginService;
    @PostMapping
    public String login(EbUser ebUser,String verycode ,HttpServletRequest request){
        System.out.println("==LoginController的login方法，ebUser:"+ebUser);

        ///0.校验验证码
        //获取原始验证码与verycode比较
        if(!verycode.equals(request.getSession().getAttribute("captcha"))){
           throw new RuntimeException("你是傻屌！！");
        }
        //1.调用service进行登录
        EbUser loginUser = loginService.login(ebUser);
        //2.不绑定数据到model,绑定到session中
        request.getSession().setAttribute("lgnUser",loginUser);
        //3.返回View
        return "redirect:index/index";// 登录成功，则返回到 网站 首页
    }

    @PostMapping("register")
    public String register(EbUser ebUser){
        System.out.println("==LoginController的register方法，ebUser:"+ebUser);


        //1.调用service进行注册(添加)
        boolean  result = loginService.register(ebUser);
        //2.不绑定数据到model,绑定到session中

        //3.返回View
        return "redirect:on";// 登录成功，则返回到 网站 首页
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }
}
